//
//  HMNavigationController.swift
//  WeiBo14
//
//  Created by HM on 16/3/2.
//  Copyright © 2016年 HM. All rights reserved.
//

import UIKit

class HMNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        
//        print(childViewControllers.count)
//        print(childViewControllers.count)
        
        
        // 只在判断里面子控制器个数大于0,就设置返回按钮
        
        if childViewControllers.count > 0 {
            
            var title = "返回"
            // 代表当前已经存在1个控制器,正要往里面push第2个,所以去获取第1个控制器的 title
            if childViewControllers.count == 1 {
                title = childViewControllers.first?.title ?? "返回"
            }
            
            // 添加自己的逻辑
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(imageNamed: "navigationbar_back_withtext", title: title, target: self, action: "back")
            viewController.hidesBottomBarWhenPushed = true
        }
        super.pushViewController(viewController, animated: true)
    }
    
    func back(){
        popViewControllerAnimated(true)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
